//
//  DTCPlistManager.m
//  WaterPurifierProduct
//
//  Created by yc on 2017/11/16.
//  Copyright © 2017年 Cong Yao. All rights reserved.
//

#import "DTCPlistManager.h"

@implementation DTCPlistManager

+ (NSMutableArray *)getArrayDataFromPlist:(NSString *)plistName modelClass:(NSString *)classString{
    
    if (plistName.length != 0 && classString.length != 0 ) {
        NSString * path = [[NSBundle mainBundle] pathForResource:plistName ofType:@".plist"];
        NSArray * array = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray * newArray  = [NSMutableArray new];
        if ([[array firstObject] isKindOfClass:[NSDictionary class]]) {
            for (NSDictionary * dic  in array) {
                
                id model = [[NSClassFromString(classString) alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [newArray addObject:model];
            }
            
        } else if ([[array firstObject] isKindOfClass:[NSArray class]]) {
            for (NSMutableArray *subArray in array) {
                NSMutableArray * newSubArray = [NSMutableArray new];
                for (NSMutableDictionary * dic  in subArray) {
                    id model = [[NSClassFromString(classString) alloc]init];
                    [model setValuesForKeysWithDictionary:dic];
                    [newSubArray addObject:model];
                }
                [newArray addObject:newSubArray];
            }
        }
        
        
        return newArray;
    }
    return @[].mutableCopy;
}



/**
 从plist里面获取模型
 @param plistName plist文件名
 @param classString 模型的名字
 @return 模型
 */
+ (id)getModelFromPlist:(NSString *)plistName modelClass:(NSString *)classString {
    if (plistName.length != 0 && classString.length != 0) {
        NSString * path = [[NSBundle mainBundle] pathForResource:plistName ofType:@".plist"];
        NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile:path];
        
        id model = [[NSClassFromString(classString) alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        return model;
    }
    return nil;
}

+ (id)getConfigModelFromPlist:(NSString *)plistName modelClass:(NSString *)classString {
    NSMutableDictionary * newDic = [NSMutableDictionary new];
    if (plistName.length != 0 && classString.length != 0) {
        NSString * path = [[NSBundle mainBundle] pathForResource:plistName ofType:@".plist"];
        NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile:path];
        
        NSArray * keyArray = dic.allKeys;
        NSArray * valueArray = dic.allValues;
        
        for (int i = 0 ; i < keyArray.count; i++) {
            NSString * key = keyArray[i];
            NSArray * newKeyArray = [key componentsSeparatedByString:@"-"];
            NSString * value = valueArray[i];
            [newDic setValue:value forKey:[newKeyArray lastObject]];
        }
        
        id model = [[NSClassFromString(classString) alloc]init];
        [model setValuesForKeysWithDictionary:newDic];
        return model;
    }
    return nil;
}

@end
